﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using com.dz.components;
namespace com.dz.web.admin
{
    public partial class bookList : BasePage
    {
        long bookId=-1;
        string bookName=string.Empty;
        int isVip=-1;
        int isExistImg=-1;
        string categoryName=string.Empty;
        string subCategoryName=string.Empty;
        string authorName=string.Empty;
        int pageIndex = 1;
        int pageSize = 20;
        int totalCount = 0;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!long.TryParse(Request["bookid"], out bookId))
                bookId = -1;
            bookName = Request["bookName"];
            if (!int.TryParse(Request["isvip"], out isVip))
                isVip = -1;
            if (!int.TryParse(Request["isExistImg"], out isExistImg))
                isExistImg = -1;
            categoryName = Request["categoryName"];
            subCategoryName = Request["subCategoryName"];
            authorName = Request["authorName"];
            if (!int.TryParse(Request["pageIndex"], out pageIndex))
                pageIndex = 1;
            if (!int.TryParse(Request["totalCount"], out totalCount))
                totalCount = 0;
            SetControlView();
            if (!IsPostBack)
            {
                BindData();
            }
        }

        private void BindData()
        {
            BookManager bookMgr = new BookManager();
            DataSet ds = bookMgr.GetBookByPage(bookId, bookName, authorName, isExistImg, categoryName, subCategoryName, isVip, pageIndex, pageSize, out totalCount);
            if (ds != null && ds.Tables.Count > 0)
            {
                rptBookList.DataSource = ds.Tables[0];
                rptBookList.DataBind();
                lblPageHtml.Text = GetPageHtml(pageIndex, pageSize, totalCount,string.Format( "bookList.aspx?bookId={0}&bookName={1}&isVip={2}&isExistImg={3}&categoryName={4}&subCategoryName={5}&authorName={6}&totalCount={7}&pageIndex=",
                    bookId,HttpUtility.UrlEncode(bookName),isVip,isExistImg,HttpUtility.UrlEncode(categoryName),HttpUtility.UrlEncode(subCategoryName),HttpUtility.UrlEncode(authorName),totalCount));
            }
        }

        private void SetControlView()
        {
            txtAuthorName.Text = authorName;
            txtBookId.Text = bookId == -1 ? "" : bookId.ToString();
            txtBookName.Text = bookName;
            drpExistImg.SelectedValue = isExistImg.ToString();
            drpVip.SelectedValue = isVip.ToString();
            txtCategoryName.Text = categoryName;
            txtSubCategoryName.Text = subCategoryName;
        }


        protected string GetBookImg(string bookId, string bookName,object isExistImage,string categoryName,string subCategoryName)
        {
            bool t_is_exist_image=false;
            if (!bool.TryParse(isExistImage.ToString(), out t_is_exist_image))
                t_is_exist_image = false;
            if (t_is_exist_image == false)
            {
                return string.Format("<img src='/bookImage/0.jpg' border='0' /><br>[{0}]-[{1}]<br>{2}",categoryName,subCategoryName, bookName);
            }
            return string.Format("<img src='/bookImage/{0}.jpg' border='0' /><br>[{1}]-[{2}]<br>{3}", bookId,categoryName,subCategoryName, bookName);
        }
    }
}
